Previous Book Contents Book Index Next

Inside Macintosh: Overview /
Chapter 8 - Menus


Adjusting Menus

At any given time during the execution of your application, it's likely that some of the commands in your menus will not be appropriate. For example, if the front window is a dialog window, then any menu commands that manipulate only document windows should be disabled. Similarly, if the desktop shows no windows belonging to your application, then the Close command in the File menu should be disabled. When a menu item is disabled, it is drawn in a dimmed text and is not highlighted when the cursor passes over it. This disabling prevents the user from choosing those commands.

An easy way to achieve this effect is to call an application-defined routine that adjusts the menus according to the current application context just before you call either MenuSelect or MenuKey. Listing 8-6 shows the version of DoMenuAdjust used by the Venn Diagrammer application.

Listing 8-6 Adjusting menus

PROCEDURE DoMenuAdjust;
   VAR
      myWindow:   WindowPtr;
      myMenu:     MenuHandle;
      count:      Integer;
BEGIN
   myWindow := FrontWindow;

   IF myWindow = NIL THEN
      DisableMenuItem(GetMenuHandle(mFile), iClose)
   ELSE
      EnableMenuItem(GetMenuHandle(mFile), iClose);

   myMenu := GetMenuHandle(mVennD);
   IF IsAppWindow(myWindow) THEN
      FOR count := 1 TO kNumTools DO
         EnableMenuItem(myMenu, count)
   ELSE
      FOR count := 1 TO kNumTools DO
         DisableMenuItem(myMenu, count);

   IF IsDAccWindow(myWindow) THEN
      EnableMenuItem(GetMenuHandle(mEdit), 0)
   ELSE
      DisableMenuItem(GetMenuHandle(mEdit), 0);
   DrawMenuBar;
END;
The DoMenuAdjust procedure calls FrontWindow to get a pointer to the frontmost window belonging to the Venn Diagrammer application. If there is no window belonging to the Venn Diagrammer application, DoMenuAdjust disables the Close menu command in the File menu. Conversely, if there is a window belonging to the application, DoMenuAdjust enables the Close command.

If the front window is a document window, then DoMenuAdjust enables all the document-specific commands in the Venn menu; otherwise, it disables all those commands. (DoMenuAdjust retrieves the menu handle by calling GetMenuHandle and passes that handle to EnableMenuItem or DisableMenuItem.)

You can disable or enable an entire menu by passing DisableMenuItem or EnableMenuItem the value 0 in place of a menu item number. This is the strategy that DoMenuAdjust follows for the Edit menu. Venn Diagrammer does no editing of its own, so DoMenuAdjust makes certain to enable the Edit menu only when a desk accessory window is frontmost. When you call DisableMenuItem or EnableMenuItem in this way, however, you also need to call the Menu Manager procedure DrawMenuBar to update the menu bar's appearance.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
9 JUL 1996